package com.sharker.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sharker.GlideImageLoader;
import com.sharker.R;
import com.sharker.models.FirstHand;
import com.sharker.models.adapter.MyAdapter;
import com.sharker.models.adapter.MyAdapter2;
import com.sharker.models.adapter.MyAdapter3;
import com.sharker.models.bean.TryData;
import com.sharker.models.data.AdBanner;
import com.sharker.models.data.CourseData;
import com.sharker.network.SharkerParams;
import com.sharker.ui.activity.ViewAllActivity;
import com.youth.banner.Banner;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link StudyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class StudyFragment extends BaseFragment implements View.OnClickListener{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private String[] images=new String[]{
            "http://img1.imgtn.bdimg.com/it/u=1794894692,1423685501&fm=23&gp=0.jpg",
            "http://img2.imgtn.bdimg.com/it/u=2934335366,916431300&fm=23&gp=0.jpg",
            "http://imgstore.cdn.sogou.com/app/a/100540002/457880.jpg"
    };
    private View view;
    private RecyclerView mStudy_rl1;
    private RecyclerView mStudy_rl3;
    private RecyclerView mStudy_rl2;

    private TextView study_mianfei;
    private TextView study_kecheng;
    private TextView study_zhuanji;

    private ImageView mImageView;

    public StudyFragment() {
        // Required empty public constructor
    }
    public static StudyFragment newInstance(String param1, String param2) {
        StudyFragment fragment = new StudyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            Log.d("TAG"+"mParam1_________",mParam1.toString());
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_study, container, false);
        view = inflater.inflate(R.layout.fragment_study,null);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (FirstHand.isHost()) {
            listBanner();
            listTry("list_try", "", 0);
            listCourse("list_course", "", 0, 1);
            listTopic("list_topic", 0);
        }
        initView();
    }

    private void initView() {

        mStudy_rl1 = (RecyclerView) view.findViewById(R.id.study_rl1);
        mStudy_rl2 = (RecyclerView) view.findViewById(R.id.study_rl2);
        mStudy_rl3 = (RecyclerView) view.findViewById(R.id.study_rl3);
        mImageView = (ImageView) view.findViewById(R.id.view_all_iv);

        mStudy_rl1.setNestedScrollingEnabled(false);
        mStudy_rl2.setNestedScrollingEnabled(false);
        mStudy_rl3.setNestedScrollingEnabled(false);
        Banner mBanner  = (Banner) view.findViewById(R.id.study_banner);
        mBanner.setImageLoader(new GlideImageLoader());
        List<String> list = Arrays.asList(images);
        mBanner.setImages(list);
        mBanner.start();
        //设置按钮的点击监听
        study_mianfei = (TextView) view.findViewById(R.id.study_text1);
        study_kecheng = (TextView) view.findViewById(R.id.study_text2);
        study_zhuanji = (TextView) view.findViewById(R.id.study_text3);
        study_mianfei.setOnClickListener(this);
        study_kecheng.setOnClickListener(this);
        study_zhuanji.setOnClickListener(this);
    }

    /**
     * 轮播图
     */
    void listBanner() {
        SharkerParams params = new SharkerParams("list_banner");
        x.http().post(params, new Callback.CommonCallback<AdBanner>() {
            @Override
            public void onSuccess(AdBanner result) {
                Log.d("TAG"+"AdBanner_______",result.toString());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 试听课程列表
     * @param uri
     * @param category
     * @param page_index
     */
    void listTry(String uri, String category, int page_index) {
        listCourse(uri, category, page_index, 0);
    }

    /**
     * 精品专辑列表
     * @param uri
     * @param page_index
     */
    void listTopic(String uri, int page_index) {
        listCourse(uri, "", page_index, 0);
    }

    /**
     * 精品课程列表
     * @param uri
     * @param category
     * @param page_index
     * @param sort_by
     */
    public void listCourse(String uri, String category, int page_index, int sort_by) {
        SharkerParams params = new SharkerParams(uri);

        if (!TextUtils.isEmpty(category)) {
            params.addBodyParameter("category", category);
        }

        params.addBodyParameter("page_size", "25");

        params.addBodyParameter("page_index", "" + page_index);

        if (sort_by != 0) {
            params.addBodyParameter("sort_by", "" + sort_by);
        }

        x.http().post(params, new Callback.CommonCallback<TryData>() {

            private GridLayoutManager girdLayoutManager;
            private MyAdapter myAdapter;

            @Override
            public void onSuccess(TryData result) {
                Log.d("TAG"+"CourseData_____",result.getTryList().toString());
                mStudy_rl1.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
                mStudy_rl1.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
                mStudy_rl1.setAdapter(new MyAdapter(result.getTryList(),getActivity()));
                //给条目设置监听
                mStudy_rl2.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
                mStudy_rl2.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
                mStudy_rl2.setAdapter(new MyAdapter2(result.getTryList(),getActivity()));
                girdLayoutManager = new GridLayoutManager(getActivity(),3);
                mStudy_rl3.setLayoutManager(girdLayoutManager);
                mStudy_rl3.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
                mStudy_rl3.setAdapter(new MyAdapter3(result.getTryList(),getActivity()));
        }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.study_text1:
                Toast.makeText(getActivity(),"点击了第一个",Toast.LENGTH_SHORT).show();
                Intent intent =new Intent(getActivity(),ViewAllActivity.class);
                startActivity(intent);
                break;
            case R.id.study_text2:
                Toast.makeText(getActivity(),"点击了第二个",Toast.LENGTH_SHORT).show();
                Intent intent2 =new Intent(getActivity(),ViewAllActivity.class);
                startActivity(intent2);
                break;
            case R.id.study_text3:
                Toast.makeText(getActivity(),"点击了第三个",Toast.LENGTH_SHORT).show();
                Intent intent3 =new Intent(getActivity(),ViewAllActivity.class);
                startActivity(intent3);
                break;

        }
    }
}
